package com.ultimatech.webapp.controller;

import com.ultimatech.webapp.UserList;
import com.ultimatech.webapp.UserModel;
import com.ultimatech.webapp.service.IWebappService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Created by 张乐平 on 7/28 0028.
 */
@Controller
public class HomeController {

    private Logger log = LoggerFactory.getLogger(HomeController.class);

    @Value("${remote.loginUrl}")
    private String loginUrl;

    @Value("${token.recCodeUrl}")
    private String recUrl;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("loginUrl",this.loginUrl);
        model.addAttribute("recCodeUrl",this.recUrl);
        return "index";
    }

    @Autowired
    private IWebappService service;

    @Value("${token.url}")
    private String tokenUrl;

    @RequestMapping(value = "/gettoken", method = RequestMethod.GET)
    public String gettoken(String code, RedirectAttributes redirectAttributes) throws IOException {

        String accessToken = this.service.getToke(code);
        if (accessToken != null) {
            redirectAttributes.addAttribute("access_token", accessToken);
            return "redirect:/main";
        } else {
            return "redirect:/";
        }
    }

    @RequestMapping("/main")
    public String main(@RequestParam("access_token") String accessToken, Model model) throws URISyntaxException {
        UserList userList = this.service.findAllUser(accessToken);
        model.addAttribute("accessToken", accessToken);
        model.addAttribute("data", userList != null ? userList.getData() : null);
        return "main";
    }

    @RequestMapping("/toadduser")
    public String toaddUser(@RequestParam("access_token") String accessToken, String id, Model model) {
        if (id != null) {
            UserModel userModel = this.service.getUserById(accessToken, Integer.valueOf(id));
            model.addAttribute("vo", userModel != null ? userModel.getData() : null);
        }
        model.addAttribute("accessToken", accessToken);
        return "toadduser";
    }

    @RequestMapping("/saveuser")
    public String saveuser(@RequestParam("access_token") String accessToken, String id, String name, String number, Model model, RedirectAttributes redirectAttributes) throws URISyntaxException {
        if (id != null && !"".equals(id.trim())) {
            this.service.updateUser(accessToken, Integer.valueOf(id), name, number);
        } else {
            this.service.createUser(accessToken, name, number);
        }
        redirectAttributes.addAttribute("access_token", accessToken);
        return "redirect:/main";
    }

    @RequestMapping("/delUser")
    public String delUser(@RequestParam("access_token") String accessToken, @RequestParam("id") String id, Model model,RedirectAttributes redirectAttributes) throws URISyntaxException {
        this.service.delUser(accessToken,Integer.valueOf(id));
        redirectAttributes.addAttribute("access_token", accessToken);
        return "redirect:/main";
    }

}
